Security News
Research
Supply Chain Attack on Rspack npm Packages Injects Cryptojacking Malware
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
@thi.ng/equiv
Advanced tools
@thi.ng/equiv is a lightweight JavaScript library for deep equality checks. It provides a robust and efficient way to compare complex data structures, including arrays, objects, sets, maps, and more. The library is designed to handle circular references and custom equality checks, making it a versatile tool for various use cases.
Basic Equality Check
This feature allows you to perform basic equality checks between primitive values. The function returns true if the values are equal and false otherwise.
const equiv = require('@thi.ng/equiv');
console.log(equiv(1, 1)); // true
console.log(equiv(1, '1')); // false
Deep Equality Check for Arrays
This feature allows you to perform deep equality checks on arrays. It compares the elements of the arrays recursively to determine if they are equal.
const equiv = require('@thi.ng/equiv');
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
const arr3 = [1, 2, 4];
console.log(equiv(arr1, arr2)); // true
console.log(equiv(arr1, arr3)); // false
Deep Equality Check for Objects
This feature allows you to perform deep equality checks on objects. It compares the properties of the objects recursively to determine if they are equal.
const equiv = require('@thi.ng/equiv');
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
const obj3 = { a: 1, b: { c: 3 } };
console.log(equiv(obj1, obj2)); // true
console.log(equiv(obj1, obj3)); // false
Handling Circular References
This feature allows you to handle circular references in objects. The library can detect and correctly compare objects with circular references.
const equiv = require('@thi.ng/equiv');
const a = {};
const b = {};
a.self = a;
b.self = b;
console.log(equiv(a, b)); // true
Custom Equality Checks
This feature allows you to define custom equality checks for your own classes or data structures. By implementing the [Symbol.for('equiv')] method, you can control how instances of your class are compared.
const equiv = require('@thi.ng/equiv');
class Custom {
constructor(value) {
this.value = value;
}
[Symbol.for('equiv')](other) {
return other instanceof Custom && this.value === other.value;
}
}
const obj1 = new Custom(1);
const obj2 = new Custom(1);
const obj3 = new Custom(2);
console.log(equiv(obj1, obj2)); // true
console.log(equiv(obj1, obj3)); // false
Lodash's `isEqual` function provides deep equality checks for JavaScript objects. It is part of the larger Lodash utility library, which offers a wide range of functions for manipulating and inspecting data. Compared to @thi.ng/equiv, lodash.isequal is more widely used and comes with the extensive support and documentation of the Lodash library.
fast-deep-equal is a small and fast library for deep equality checks. It is optimized for performance and has a very small footprint. While it offers similar functionality to @thi.ng/equiv, it may not handle as many edge cases or provide as much flexibility for custom equality checks.
deep-equal is another library for deep equality checks. It is simple to use and provides a straightforward API for comparing complex data structures. Compared to @thi.ng/equiv, deep-equal is more focused on simplicity and ease of use, but may not offer the same level of customization or performance.
[!NOTE] This is one of 199 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️
Extensible deep value equivalence checking for any data types.
Supports:
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/equiv
ESM import:
import * as equiv from "@thi.ng/equiv";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/equiv"></script>
For Node.js REPL:
const equiv = await import("@thi.ng/equiv");
Package sizes (brotli'd, pre-treeshake): ESM: 436 bytes
None
import { equiv } from "@thi.ng/equiv";
equiv(
{ a: { b: [1, 2] } },
{ a: { b: [1, 2] } }
);
// true
This is useful & required for custom types to take part in equiv
checks, by default only plain objects & array are traversed deeply.
Furthermore, by implementing this interface we can better control which
internal values / criteria are required to establish equivalence. In
this example we exclude the meta
property and only check for same type
& children
equality.
import { IEquiv } from "@thi.ng/api";
import { equiv } from "@thi.ng/equiv";
class Node implements IEquiv {
meta: any;
children: any[];
constructor(children: any[], meta?: any) {
this.children = children;
this.meta = meta;
}
equiv(o: any) {
return o instanceof Node && equiv(this.children, o.children);
}
}
equiv(new Node([1,2,3], "foo"), new Node([1,2,3], "bar"));
// true
If this project contributes to an academic publication, please cite it as:
@misc{thing-equiv,
title = "@thi.ng/equiv",
author = "Karsten Schmidt",
note = "https://thi.ng/equiv",
year = 2016
}
© 2016 - 2024 Karsten Schmidt // Apache License 2.0
FAQs
Extensible deep value equivalence checking for any data types
We found that @thi.ng/equiv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.
Security News
Sonar’s acquisition of Tidelift highlights a growing industry shift toward sustainable open source funding, addressing maintainer burnout and critical software dependencies.